package net.pet.controller.admin;


import javax.servlet.http.HttpServletRequest;

import net.pet.biz.admin.AdminUserService;
import net.pet.commons.helper.SessionHelper;
import net.pet.dto.admin.AdminUser;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class LoginController {
	@Autowired
	private AdminUserService adminUserService;
	
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String login(ModelMap modelMap,HttpServletRequest request,AdminUser user) throws Exception {
		boolean login = adminUserService.login(user);
		if(login){
			//查询用户权限
			user = adminUserService.getMenus(user);
			//将user放入session
			SessionHelper.setAdminUser(user,request);
			return "redirect:/main.htm";
		}
		modelMap.put("login","0");
		modelMap.put("msg","用户信息不匹配");
		return "/index";
	}
	@RequestMapping(value = "/logout", method = RequestMethod.GET)
	public String logout(ModelMap modelMap,HttpServletRequest request) throws Exception {
		SessionHelper.invalidateSession(request);
		return "/index";
	}
	
}
